package 数据结构.sort;

import java.util.Arrays;

/**
 * 快速排序
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {23, 3, 3, 23, -567, 3, 2, 23};
        // int[] arr = {1, 3, 1, 4, 5};
        quickSort(arr, 0, 7);
        System.out.println(Arrays.toString(arr));
    }

    public static void quickSort(int[] arr, int left, int right) {
        int temp = 0;
        int l = left;
        int r = right;
        int middle = arr[(left + right) / 2];

        while (l < r) {
            while (arr[l] < middle) {
                l++;
            }
            while (arr[r] > middle) {
                r--;
            }
            if (l == r) {
                break;
            }
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            if (arr[l] == middle) {
                r -= 1;
            }
            if (arr[r] == middle) {
                l += 1;
            }
        }
        if (l == r) {
            l += 1;
            r -= 1;
        }
//        System.out.println("r:" + r);
//        System.out.println(l);
        if (left < r) {
            quickSort(arr, left, r);
        }
        if (right > l) {
            quickSort(arr, l, right);
        }

    }
}
